# coding=utf-8
import requests
from bs4 import BeautifulSoup

class JobReptile(object):
    """ 爬去51job职业搜索页面，工资"""

    @staticmethod
    def get_html(url,encoding):
        """get the content of the url"""

        print("请求的url:"+url)

        response = requests.get(url)
        response.encoding = encoding
        return response.text



    @staticmethod
    def get_all_job_links(html):
        """ 获取所有查询结果的link"""

        link_list = []
        soup = BeautifulSoup(html,'lxml')
        jobs_list = soup.select(".el")
        for job in jobs_list:
            link_address = job.select("span")[0].select("a")
            if len(link_address) > 0:
                attr_dic = dict(link_address[0].attrs)
                link_list.append(attr_dic['href'])
        return link_list



    @staticmethod
    def get_salary(html):
        """获取所有工资,并且写入到txt文件"""

        soup = BeautifulSoup(html, 'lxml')
        jobs_list = soup.select(".el")
        file = open("salary.txt","a+")
        for job in jobs_list:
            if len(job.select("span")) > 4:
                salary = job.select("span")[3].string
                if salary is not None and salary != '地区地标' and salary != '薪资':
                    file.write(salary+"\n")
                    #print(salary)


    @staticmethod
    def read_file(file):
        """读取文件，并且简单统计 """

        dic = dict()
        count = 0;
        for line in open(file):
            line = line.replace("\n","")
            if len(line) > 0 and line in dic:
                count = dic[line]
                count += 1
                dic[line] = count
            else:
                dic[line] = 1
        return dic


    @staticmethod
    def get_all_job_descripton(links):
        """获取职位描述，并且写入txt文件"""

        file = open("description.txt","a+",encoding="utf-8")
        index = 1
        for link in links:
            print("请求第"+str(index)+"个")
            index+=1
            html = JobReptile.get_html(link,"gbk")
            soup = BeautifulSoup(html,'lxml')

            job_msg_list = soup.select(".job_msg")
            if len(job_msg_list) > 0:
                job_msg = soup.select(".job_msg")[0].get_text()
                file.write(job_msg)
